import 'package:flutter/material.dart';

class CounterTextWidgt extends StatelessWidget {
  const CounterTextWidgt({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var bloc2 = CounterProvider.of(context);
    print("CounterTextWidgt build: $bloc2");
    return Center(
      child: Text("${bloc2.count}"),
    );
  }
}

class ActionBarWidget extends StatelessWidget {
  const ActionBarWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var bloc = CounterProvider.of(context);
    print("ActionBarWidget build: $bloc");
    return FloatingActionButton(
      onPressed: () {
        bloc.incremnt();
      },
      child: Icon(Icons.add),
    );
  }
}

class CounterProvider extends InheritedWidget {
  final int count;
  final VoidCallback incremnt;

  CounterProvider({
    required this.count,
    required this.incremnt,
    Key? key,
    required Widget child,
  }) : super(key: key, child: child);

  static CounterProvider of(BuildContext context) {
    final CounterProvider? result =
        context.dependOnInheritedWidgetOfExactType<CounterProvider>();
    assert(result != null, 'No CounterProvider found in context');
    return result!;
  }

  @override
  bool updateShouldNotify(CounterProvider old) {
    return true;
  }
}
